home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************************
- BetterBitStream.c
-
- Copyright © 1999 Red Shed Software. All rights reserved.
- by Jonathan 'Wolf' Rentzsch (jon@redshed.net)
-
- Commenter Date Comment
- --------- ----------------- -----------------------------------------------------
- wolf Thu, Mar 18, 1999 Created.
-
- ************************************************************************************/
-
- #include "BetterBitStream.h"
-
- /****************************************************************************************
- Commenter Date Comment
- --------- ----------------- -----------------------------------------------------
- wolf Tue, Mar 16, 1999 Created.
-
- ************************************************************************************/
-
- void
- InsertBetterBitStream(
- void **bitStream, // -> A pointer to a pointer to a buffer.
- // <- Incremented with each filled byte.
- UInt32 *bitPosition, // -> Where to put the bit.
- // <- Incremented.
- UInt8 bit ) // -> What value to insert.
- {
- typedef struct { UInt8 set, clear; } Operation;
- static Operation opTable[ 8 ] = { 1 << 7, ~(1 << 7),
- 1 << 6, ~(1 << 6),
- 1 << 5, ~(1 << 5),
- 1 << 4, ~(1 << 4),
- 1 << 3, ~(1 << 3),
- 1 << 2, ~(1 << 2),
- 1 << 1, ~(1 << 1),
- 1 << 0, ~(1 << 0) };
- UInt8 *byteStream = (UInt8*) *bitStream;
- UInt8 bitPositionLow3Bits = (*bitPosition & 0x00000007);
- Operation *operation = &opTable[ bitPositionLow3Bits ];
-
- if( bit == 0x00 )
- *byteStream &= operation->clear;
- else
- *byteStream |= operation->set;
- if( bitPositionLow3Bits == 7 ) // This byte is full, increment it.
- *bitStream = ++byteStream;
- ++(*bitPosition);
- }
-
- /****************************************************************************************
- Commenter Date Comment
- --------- ----------------- -----------------------------------------------------
- wolf Thu, Mar 18, 1999 Created.
-
- ************************************************************************************/
-
- UInt8 // <- The retrieved bit.
- GetBetterBitStream(
- void **bitStream, // -> A pointer to a pointer to a buffer.
- // <- When a byte is emptied, the pointer is
- // automatically incremented.
- UInt32 *bitPosition ) // -> Current position in the buffer.
- // <- Automatically incremented.
- {
- static UInt8 opTable[ 8 ] = { 1 << 7,
- 1 << 6,
- 1 << 5,
- 1 << 4,
- 1 << 3,
- 1 << 2,
- 1 << 1,
- 1 << 0 };
- UInt8 *byteStream = (UInt8*) *bitStream;
- UInt8 bitPositionLow3Bits = (*bitPosition & 0x00000007);
- UInt8 bit = (*byteStream) & opTable[ bitPositionLow3Bits ];
-
- if( bitPositionLow3Bits == 7 ) // This byte is empty, increment it.
- *bitStream = ++byteStream;
- ++(*bitPosition);
- return( bit == 0x00 ? 0x00 : 0x01 );
- }